package com.chaihu.order.service.impl;

import com.chaihu.bean.dto.order.MappingDto;
import com.chaihu.bean.entity.center.AppRoute;
import com.chaihu.core.cache.AppRouteCache;
import com.chaihu.core.cache.MappingCache;
import com.chaihu.order.dao.MappingServiceDao;
import com.chaihu.order.dao.RouteServiceDao;
import com.chaihu.order.service.CenterServiceCacheService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chaihu
 * @date 2021/10/13
 */
@Slf4j
@Service("centerServiceCacheService")
public class CenterServiceCacheServiceImpl implements CenterServiceCacheService {

    @Autowired
    private MappingServiceDao mappingServiceDao;
    @Autowired
    private RouteServiceDao routeServiceDao;

    @Override
    public void flush(Map<String, String> headers) {
        // TODO
    }

    @Override
    public void startFlush() {
        //1.0 封装 AppRoute
        doFlushAppRoute();

        //2.0 分装 Mapping
        doFlushMapping();
    }

    private void doFlushAppRoute() {
        log.debug("开始刷新 AppRoute数据到redis数据库中");
        List<AppRoute> appInfos = routeServiceDao.getAppRouteAndService();
        Map<String, List<AppRoute>> appRoustsMap = new HashMap<>();
        List<AppRoute> appRoutes = null;
        for (AppRoute appInfo : appInfos) {
            if (appRoustsMap.containsKey(appInfo.getAppId())) {
                appRoutes = appRoustsMap.get(appInfo.getAppId());
                appRoutes.add(appInfo);
            } else {
                appRoutes = Lists.newArrayList(appInfo);
                appRoustsMap.put(appInfo.getAppId(), appRoutes);
            }
        }
        //删除原始数据
        AppRouteCache.removeData(AppRouteCache._SUFFIX_APP_ROUTE);

        for (String appId : appRoustsMap.keySet()) {
            AppRouteCache.setAppRoute(appRoustsMap.get(appId));
        }
    }

    private void doFlushMapping() {
        log.debug("开始刷新 Mapping数据到redis数据库中");
        List<MappingDto> mappings = mappingServiceDao.selectMappingAll();
        //删除原始数据
        MappingCache.removeData(MappingCache._SUFFIX_MAPPING);
        for (MappingDto mapping : mappings) {
            MappingCache.setValue(mapping);
        }

        Map<String, List<MappingDto>> mappingMap = new HashMap<>();
        List<MappingDto> mappingsNew = null;
        for (MappingDto mapping : mappings) {
            if (mappingMap.containsKey(mapping.getDomain())) {
                mappingsNew = mappingMap.get(mapping.getDomain());
                mappingsNew.add(mapping);
            } else {
                mappingsNew = Lists.newArrayList(mapping);
                mappingMap.put(mapping.getDomain(), mappingsNew);
            }
        }

        for (String domain : mappingMap.keySet()) {
            MappingCache.setValue(mappingMap.get(domain));
        }
    }
}
